import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pet_app/base/base_controller.dart';
import 'package:pet_app/base/base_list_controller.dart';
import 'package:pet_app/model/article.dart';
import 'package:pet_app/model/enums/article_type.dart';
import 'package:pet_app/model/enums/pet_type.dart';
import 'package:pet_app/model/query/article_query.dart';
import 'package:pet_app/net/http.dart';
import 'package:pet_app/routes/router.dart';
import 'package:pet_app/widget/loading_widget.dart';

class ArticleLogic extends BaseListController {
  List<Article> articleList = [];
  late Color mainColor;
  late ArticleType type;
  late PetType petType;
  late String name;

  @override
  void initData() {
    type = Get.arguments['type'];
    petType = Get.arguments['petType'];
    getNetworkData(configNetworkParams());
  }

  @override
  dynamic configNetworkParams() {
    switch (type) {
      case ArticleType.breed:
        name = '品种分组';
        mainColor = Colors.amberAccent;
        break;
      case ArticleType.health:
        name = '健康';
        mainColor = Colors.redAccent;
        break;
      case ArticleType.diet:
        name = '营养饮食';
        mainColor = Colors.deepOrangeAccent;
        break;
      case ArticleType.training:
        name = '行为训练';
        mainColor = Colors.blueAccent;
        break;
      case ArticleType.breedSearch:
        name = '品种查询';
        mainColor = Colors.orangeAccent;
        break;
    }
    return ArticleQuery(type.value,petType.value)..pageNum = page;
  }

  @override
  void getNetworkData(dynamic params) {
    super.getNetworkData(params);
    Loading.show();
    Http().client.queryPageArticle(params).then((resp) {
      netState = NetState.dataSussessState;
      Loading.dismiss();
      List<Article> list = resp.data?.list ?? [];
      if (page == 1) {
        articleList = list;
        refreshController.refreshCompleted();
      } else {
        articleList.addAll(list);
        refreshController.loadComplete();
      }
      if (articleList.isEmpty) {
        netState = NetState.emptyDataState;
      }
      if (list.length < 10) {
        refreshController.loadNoData();
      }
      update();
    });
  }

  @override
  void loadMore() {
    super.loadMore();
    page += 1;
    getNetworkData(configNetworkParams());
  }

  @override
  void refreshData() {
    super.refreshData();
    page = 1;
    getNetworkData(configNetworkParams());
  }

  toArticleDetailPage(Article article) {
    Get.toNamed(AppRoutes.articleDetail, arguments: {'article': article});
  }
}
